package org.insidesource.flexbugs.dao.impl;

import java.util.List;

import org.insidesource.flexbugs.dao.CommentDao;
import org.insidesource.flexbugs.model.Comment;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class CommentDaoJdbcImpl extends SimpleJdbcDaoSupport implements CommentDao
{
	public void deleteAllCommentsForIssueId(Long issueId)
	{
	}

	public List<Comment> getCommentsByIssueId(Long issueId)
	{
		String sql = "select * from comment where issueId=" + issueId;
		
		return this.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Comment.class));
	}

	public Comment get(Long id)
	{
		String sql = "select * from comment where id=" + id;
		return this.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Comment.class)).get(0);
	}

	public void remove(Long id)
	{
		String sql = "delete from comment where id=" + id;
		this.getSimpleJdbcTemplate().update(sql);
	}

	public void save(Comment comment)
	{
		String sql = "insert into comment(issue_id, author, createDate, commentText) value(?, ? , now(), ?)";
		this.getSimpleJdbcTemplate().update(sql, comment.getIssue().getId(), comment.getAuthor(), comment.getCommentText());
	}
}
